package com.autumn.sms.configure;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.autumn.sms.SmsChannel;
import com.autumn.sms.aliyun.AliyunSmsChannel;
import com.autumn.sms.configure.AutumnSmsProperties.AliyunSmsProperties;
import com.autumn.sms.configure.AutumnSmsProperties.TencentSmsProperties;
import com.autumn.sms.tencent.TencentSmsChannel;

/**
 * 短信配置
 * 
 * @author 杨昌国 2018-12-08 00:21:54
 */
@Configuration
@EnableConfigurationProperties({ AutumnSmsProperties.class })
public class AutumnSmsConfiguration {

	/**
	 * 短信通道_阿里云
	 */
	public static final String BEAN_SMS_CHANNEL_ALIYUN = "aliyunSmsChannel";

	/**
	 * 短信通道_腾讯云
	 */
	public static final String BEAN_SMS_CHANNEL_TENCENT = "tencentSmsChannel";

	/**
	 * 阿里云
	 * 
	 * @param properties
	 *            属性
	 * @return
	 */
	@Bean(BEAN_SMS_CHANNEL_ALIYUN)
	@ConditionalOnProperty(name = AliyunSmsProperties.BEAN_CONDITIONAL_PROPERTY, havingValue = "true")
	@ConditionalOnMissingBean(AliyunSmsChannel.class)
	public SmsChannel aliyunSmsChannel(AutumnSmsProperties properties) {
		AliyunSmsChannel channel = new AliyunSmsChannel();
		channel.setAccessKeyId(properties.getAliyun().getAccessKeyId());
		channel.setAccessKeySecret(properties.getAliyun().getAccessKeySecret());
		channel.setDefaultSignName(properties.getAliyun().getDefaultSignName());
		return channel;
	}

	/**
	 * 腾讯云
	 * 
	 * @param properties
	 *            属性
	 * @return
	 */
	@Bean(BEAN_SMS_CHANNEL_TENCENT)
	@ConditionalOnProperty(name = TencentSmsProperties.BEAN_CONDITIONAL_PROPERTY, havingValue = "true")
	@ConditionalOnMissingBean(TencentSmsChannel.class)
	public SmsChannel tencentSmsChannel(AutumnSmsProperties properties) {
		TencentSmsChannel channel = new TencentSmsChannel();
		channel.setAppid(properties.getTencent().getAppId());
		channel.setAppkey(properties.getTencent().getAppKey());
		return channel;
	}

}
